﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace SimpleTaskManager.Domain.Entities
{
    /// <summary>
    /// Meta informations about a report
    /// </summary>
    public class ReportHeader
    {
        /// <summary>
        /// ID
        /// </summary>
        public int ReportHeaderID { get; set; }

        /// <summary>
        /// The name of the report, chosen by the uploader.
        /// </summary>
        [Required(ErrorMessage = "Please enter a name for the report")]
        public string Name { get; set; }

        /// <summary>
        /// The time when the report was created
        /// </summary>
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime CreationDate { get; set; }

        /// <summary>
        /// The reference to the task to which the report belongs
        /// </summary>
        [InverseProperty("Reports")]
        public virtual Task Task { get; set; }

        /// <summary>
        /// The ID of the user who created the report
        /// </summary>
        public int CreatedById { get; set; }

        /// <summary>
        /// The reference to the user who created the report
        /// </summary>
        [InverseProperty("CreatedReports")]
        [ForeignKey("CreatedById")]
        public virtual User CreatedBy { get; set; }

        /// <summary>
        /// Indicates whether a report have been approved
        /// </summary>
        [NotMapped]
        public bool IsApproved
        {
            get
            {
                //A report is approved if a user is set who approved it
                return (ApprovedBy != null);
            }
        }

        /// <summary>
        /// The ID of the user who approved the report.
        /// <remarks>After a report is uploaded it is null, indicating the report haven't been approved yet.</remarks>
        /// </summary>
        public int? ApprovedById { get; set; }

        /// <summary>
        /// The reference to the user who approved the report.
        /// </summary>
        [InverseProperty("ApprovedReports")]
        [ForeignKey("ApprovedById")]
        public virtual User ApprovedBy { get; set; }

        /// <summary>
        /// The reference to the report data
        /// </summary>
        [InverseProperty("Header")]
        public virtual ReportDetail Details { get; set; }

        /// <summary>
        /// Extend the report containing only a name with required informations.
        /// </summary>
        /// <param name="file">The uploaded report file</param>
        /// <param name="uploader">The uploader user</param>
        /// <returns>The prepared report</returns>
        public ReportHeader PrepareReport(UploadedFile file, User uploader)
        {
            //Prepare the report
            CreationDate = DateTime.Now;
            CreatedById = uploader.UserID;
            ApprovedBy = null;
            //Create the details
            Details = new ReportDetail 
            { 
                FileName = file.Filename, 
                Data = file.Contents, 
                ContentType = file.ContentType 
            };

            return this;
        }
    }
}
